package cn.mumzone.pos.ui.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.mumzone.basic.adapter.rv.CommonAdapter;
import cn.mumzone.basic.adapter.rv.ViewHolder;
import cn.mumzone.basic.util.BigDecimalUtil;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.basic.util.ToastUtil;
import cn.mumzone.basic.util.log.Elog;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.GoodsEntity;
import cn.mumzone.pos.model.MemberEntity;
import cn.mumzone.pos.util.GoodsUtil;

/**
 * Created by qiuzhenhuan on 2019/6/14.
 */

public class ADA_GoodsSoldList extends CommonAdapter<GoodsEntity> {

    private MemberEntity memberEntity;

    public ADA_GoodsSoldList(Context context) {
        super(context);
    }

    public void setBindMember(MemberEntity bindMember) {
        memberEntity = bindMember;
    }

    @Override
    protected int itemLayoutId() {
        return R.layout.item_discounted_goods;
    }

    @Override
    protected void convert(ViewHolder holder, final GoodsEntity goodsEntity, final int position) {

        holder.setText(R.id.tv_good_name, goodsEntity.getGoodsName());

        holder.setText(R.id.tv_good_spec, getGoodSpecString(goodsEntity.getGoodsSpec(), goodsEntity.getUnitName()));

        TextView tvOriginPrice = holder.getView(R.id.tv_good_origin_price);
        TextView tvPrice = holder.getView(R.id.tv_good_price);

        // 原价，默认划掉
        tvOriginPrice.setText("￥" + goodsEntity.getPrice());
        tvOriginPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);

        String selectPrice = GoodsUtil.getGoodsPriceByMember(memberEntity, goodsEntity);
        tvPrice.setText(StringUtils.getFormattedMoneyWithUnit(selectPrice));
        if (selectPrice.equals(goodsEntity.getPrice())) {
            tvOriginPrice.setVisibility(View.GONE);
        } else {
            tvOriginPrice.setVisibility(View.VISIBLE);
        }


        holder.setVisible(R.id.tv_select_count, goodsEntity.getSelectCount() != 0);
        holder.setText(R.id.tv_select_count, StringUtils.getString(goodsEntity.getSelectCount()));

        holder.getConvertView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onItemClicked(position);
                }
            }
        });

    }

    private String getGoodSpecString(List<GoodsEntity.GoodsSpecBean> goodsSpec, String unitName) {
        StringBuilder builder = new StringBuilder();
        builder.append("规格：");
        if (!ListUtil.isListEmpty(goodsSpec) || !TextUtils.isEmpty(unitName)) {
            for (int i = 0; i < goodsSpec.size(); i++) {
                GoodsEntity.GoodsSpecBean goodsSpecBean = goodsSpec.get(i);
                builder.append(goodsSpecBean.getValue());
                builder.append(",");
            }
            builder.append(unitName);
        } else {
            builder.append("暂无");
        }
        return builder.toString();
    }

    public interface OnItemClickedListener {
        void onItemClicked(int position);
    }

    private OnItemClickedListener mListener;

    public void setOnItemClickedListener(OnItemClickedListener mListener) {
        this.mListener = mListener;
    }

}
